//
// Created by Administrator on 2015/10/22 0022.
//

#include "com_jni_zlib_ZipFileUtils.h"

JNIEXPORT void JNICALL Java_com_jni_zlib_ZipFileUtils_zipFile
        (JNIEnv *env, jclass obj, jstring filePath) {
    //转换成本地的char
    const char *file_char = (*env)->GetStringChars(env, filePath, NULL);
    __android_log_write(ANDROID_LOG_ERROR,"file_path",file_char);
    if (file_char == NULL) {
        return;
    }
    unzip(file_char);
    int i = 0;
    int result;
    __android_log_write(ANDROID_LOG_INFO, LOG_TAG, file_char);
    //释放内存
    (*env)->ReleaseStringChars(env, filePath,file_char);

}
void error_quit(const char *msg)
{
    __android_log_write(ANDROID_LOG_INFO,LOG_TAG,msg);
    perror(msg);
    exit(-1);
}

void memcat(char *tmp,char *buf,unsigned long len)
{
    int i=2,j=0;

    while(len)
    {
        tmp[i]=buf[j];

        ++i;
        ++j;
        --len;
    }
}

int get_zip_file_num(char *zip_file)
{
    int i=0;
    FILE *fp;
    struct data zip;

    if((fp=fopen(zip_file,"rb"))==NULL)
        return -1;

    while(!feof(fp))
    {
        fread(&zip,sizeof(struct data),1,fp);
        if(zip.f_len <= 0)
            break;
        fseek(fp,zip.f_len,SEEK_CUR);

        if(zip.e_len > 0)
            fseek(fp,zip.e_len,SEEK_CUR);

        if(zip.d_len > 0)
            fseek(fp,zip.d_len,SEEK_CUR);

        ++i;
    }

    fclose(fp);

    return i;
}

void unzip(char *zip_file)
{
    struct data zip;
    FILE *fp,*out;
    char *buf,*source,*name,*tmp;
    unsigned short s=0x9c78;
    unsigned long blen;

    if((fp=fopen(zip_file,"rb"))==NULL)
        error_quit("Open File");

    while(!feof(fp))
    {
        fread(&zip,sizeof(struct data),1,fp);
        if(zip.f_len <= 0)
            break;
        name=malloc(zip.f_len+1);
        bzero(name,zip.f_len+1);
        fread(name,zip.f_len,1,fp);

        if(zip.e_len > 0)
            fseek(fp,zip.e_len,SEEK_CUR);

        if(zip.d_len == 0)
            mkdir(name,0777);
        else if(zip.d_len == zip.len)
        {
            if((out=fopen(name,"w"))==NULL)
                error_quit("Create File");

            printf("Unzip %s . . .\n",name);

            buf=malloc(zip.d_len+1);
            bzero(buf,zip.d_len+1);

            fread(buf,zip.d_len,1,fp);
            fwrite(buf,zip.d_len,1,out);

            fclose(out);
            free(buf);

            printf("Unzip %s Successed . . .\n",name);
        }
        else
        {
            buf=malloc(zip.d_len+1);
            bzero(buf,zip.d_len+1);
            fread(buf,zip.d_len,1,fp);

            source=malloc(zip.len+1);
            bzero(source,zip.len+1);

            tmp=malloc(zip.d_len+3);
            bzero(tmp,zip.d_len+3);
            memcpy(tmp,&s,sizeof(s));
            memcat(tmp,buf,zip.d_len);

            if((out=fopen(name,"w"))==NULL)
                error_quit("Create File");

            printf("Unzip %s . . .\n",name);

            blen=compressBound(zip.len);
            uncompress(source,&blen,tmp,zip.len);

            fwrite(source,zip.len,1,out);

            fclose(out);
            free(source);
            free(tmp);
            free(buf);

            printf("Unzip %s Successed . . .\n",name);
        }

        free(name);
    }

    fclose(fp);
}

